import { STATUS, MSG, DATA } from './http-config';
import service from './http-axios';

export class HttpResult<T> {
  status: number = 0;
  msg: string = '';
  data: T;

  constructor(status: number, msg: string, data: T) {
    this.status = status;
    this.msg = msg;
    this.data = data;
  }
}

//统一处理返回值
function getHttpResult<T>(responsData: any) {
  const status = responsData[STATUS];
  const msg = responsData[MSG];
  const data = responsData[DATA];
  return new HttpResult<T>(status, msg, data);
}

export async function getRequest<T>(url: string, params: any = null): Promise<HttpResult<T>> {
  let responsData: any = await service.get(url, params);
  return getHttpResult<T>(responsData);
}

export async function postRequest<T>(url: string, params: any = null): Promise<HttpResult<T>> {
  let responsData: any = await service.post(url, params);
  return getHttpResult<T>(responsData);
}
